package com.bkck.sharkblockade.levels.shark;

import com.bkck.sharkblockade.levels.shark.Shark;
import android.graphics.Bitmap;
import android.graphics.Canvas;

/**
* The animation when a shark dies.
*
* @author		Team BKCK
*
*/

public class SharkDeath
{
	// An array of bitmap images to draw for the animation
	private Bitmap[] images;

	// The coordinates of the animation
	private float x;
	private float y;

	// The dimensions of the animation
	private int width;
	//private int height;

	// Frame duration in milliseconds
	private int frameDuration = 100;

	// Variables needed to check the actual frame duration.
	private long lastUpdateTime = -1;
	private long elapsedTime = 0;

	// The current image
	private int currentImage;

	// Whether or not the shark is dying
	private boolean sharkDying;

	private Shark shark;
	  
	/**
	 * The constructor.
	 * 
	 * @param images The array of bitmap images for the animation
	 * 
	 */

	public SharkDeath(Bitmap[] images) {
		this.images = images;
	    this.width = images[0].getWidth();
	    //this.height = images[0].getHeight();
	    this.x = 0;
	    this.y = 0;
	    this.currentImage = 0;
	    this.sharkDying = false;
	}
	  
	/**
	 * Returns true if the shark is dying or dead.
	 * 
	 * @return dying		A boolean true if the shark is dying or dead,
	 * 						false otherwise.
	 * 
	 */
	  
	public boolean isDying() {
		return (this.sharkDying);
	}
	  
	/**
	 * Start the dying animation.
	 * 
	 * @param shark		The dying shark.
	 * 
	 */
	  
	public void startDying(Shark shark) {
		this.shark = shark;
		this.x = shark.getX() - (this.width - shark.getWidth());
		this.y = shark.getY();
		this.sharkDying = true;  
	}
	
	/**
	 * Updates the animation.
	 * 
	 */

	public void update() {
	  // Update only if the shark is dying
	  if (this.sharkDying == true) {
	    // Update time variables
	    long currentTime = System.currentTimeMillis();

	    // If the animation is still ongoing
	    // (at least, probably what this condition means;
	    // still trying to understand Explosion.java
	    // and EatBall.java, and most of this project really)
	    if (this.lastUpdateTime >= 0) {
	      this.elapsedTime = this.elapsedTime + currentTime - this.lastUpdateTime;
	      this.lastUpdateTime = currentTime;
	      
	      // If the elapsed time is greater than the frame duration,
	      // then it is time to move to the next frame
	      if (elapsedTime >= frameDuration) {
	        // End once the animation is completed
	        if (currentImage == 6) {
	          if (shark.getColor() == Shark.BLUE) {
	            shark.setXVelocity(-10);
	          }
	          if (shark.getColor() == Shark.ORANGE) {
	            shark.setXVelocity(10);
	          }
	          endDying();
	        }

	        // Otherwise update the images
	        else {
	          elapsedTime = 0;
	          currentImage++;
	        }
	      }
	    }
	    else {
	      this.lastUpdateTime = currentTime;
	    }
	  }
	}

	/**
	 * End the dying animation.
	 * 
	 */

	public void endDying() {
	  this.currentImage = 0;
	  this.sharkDying = false;
	  this.lastUpdateTime = -1;
	}

	/**
	 * Draw the animation on the canvas.
	 * 
	 * @param canvas The canvas to draw on.
	 * 
	 */

	public void draw(Canvas canvas) {
	  if (this.sharkDying == true) {
	    canvas.drawBitmap(this.images[this.currentImage], this.x, this.y, null);
	  }
	}
	  
}